package com.xiao.community.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xiao.community.util.OSSFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.community.entity.GoodsEntity;
import com.xiao.community.service.GoodsService;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author 韩国涛
 * @email 18862701405@gmail.com
 * @date 2021-09-10 15:22:54
 */
@RestController
@RequestMapping("community/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @Scheduled(cron = "0/5 * * * * *")
    @RequestMapping("/bulkeslist")
    public void esadd() throws IOException {
         goodsService.addes();

    }

    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        String s = OSSFileUtil.uploadFile(file);

        return R.ok().put("oss", s);
    }

    @RequestMapping("selectByGoodsId")
    public List<GoodsEntity> goodsEntities(){

        return goodsService.selectByGoodsIfsale(0);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    public R info(@PathVariable("goodsId") Integer goodsId){
		GoodsEntity goods = goodsService.getById(goodsId);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] goodsIds){
		goodsService.removeByIds(Arrays.asList(goodsIds));

        return R.ok();
    }

}
